package jllowrey.view;

import java.awt.event.ActionListener;
import java.util.EventListener;

import javax.jws.Oneway;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import jllowrey.controller.BeginButtonController;
import jllowrey.controller.ChoiceController;
import jllowrey.controller.DrawEdgeController;
import jllowrey.model.DecisionLinesEvent;

/**
 * Default application layout for A1.impl;
 * 
 * Feel free to use but leave attribution in place.
 * 
 * @author heineman
 */
public class Application extends JFrame {

	/** Maintain reference to mecisionLinesEvent. */
	DecisionLinesEvent decisionLinesEvent;
	
	/** Relevant Boundary objects. */
	JPanel contentPane;
	private JPanel choicesPanel;
	private DrawingPanel drawingPanel;
	JLabel status;
	
	/** Choices here. */
	public JTextField[] choices;
	
	JButton button;
	
	/**
	 * Sets up the application window and drawing canvas. Also installs controllers.
	 */
	public Application(DecisionLinesEvent m) {
		this.decisionLinesEvent = m;
		
		setTitle("Decision Lines Event SAMPLE");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 500, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		setChoicesPanel(new JPanel());
		getChoicesPanel().setLayout(null);
		getChoicesPanel().setBounds(10, 11, 464, 54);
		contentPane.add(getChoicesPanel());
		
		// add choice text fields in place.
		choices = new JTextField[decisionLinesEvent.numChoices];
		int width = 464/decisionLinesEvent.numChoices;
		for (int i = 0; i < decisionLinesEvent.numChoices; i++) {
			choices[i] = new JTextField(10);
			choices[i].setBounds(i*width, 10, width - 20, 24);
			getChoicesPanel().add(choices[i]);
			
			
			
			ChoiceController choiceControl = new ChoiceController(decisionLinesEvent, i, this);
			choices[i].addActionListener(choiceControl);

	
			// here is where you would consider registering a controller with each
			// choices[i] JTextField. When entering text into a choices[i] text field
			// and pressing return, your controller would be executed
		}
		
		setDrawingPanel(new DrawingPanel(decisionLinesEvent));
		getDrawingPanel().setBounds(10, 76, 464, 334);
		contentPane.add(getDrawingPanel());
				
		
		// here is where you would considering registering controllers to process
		// mouse events in the Drawing Panel.
		
		JButton beginButton = new JButton("Begin");
		beginButton.setBounds(10, 428, 89, 23);
		contentPane.add(beginButton);
		
		button = beginButton;		
		button.setVisible(false);
		
		BeginButtonController beginControl  = new BeginButtonController(decisionLinesEvent, this);
		getButton().addActionListener(beginControl);
		
		// here is where you would consider registering a controller to process 
		// action events when user clicks on the button.
		
		status = new JLabel("Try Me");
		status.setForeground(java.awt.Color.red);
		status.setBounds(109, 428, 360, 23);
		contentPane.add(status);
		
		// call status.setText(text) when you want to announce an error/warning; call this same method
		// with "" as parameter to clear the status information.
	}
	/** getter for the button*/
	public JButton getButton() {
		return button;
	}

	/** setter*/
	public void setButton(JButton button) {
		this.button = button;
	}

	/** Make it possible for controllers to get access to status messsage. */
	public JLabel getStatus() {
		return status;
	}
	/** getter*/
	public DrawingPanel getDrawingPanel() {
		return drawingPanel;
	}

	/** setter*/
	public void setDrawingPanel(DrawingPanel drawingPanel) {
		this.drawingPanel = drawingPanel;
	}
	/** getter*/
	public JPanel getChoicesPanel() {
		return choicesPanel;
	}
	/** setter*/
	public void setChoicesPanel(JPanel choicesPanel) {
		this.choicesPanel = choicesPanel;
	}
}
